﻿using IBaseRepository;
using SqlSugar;
using SqlSugarAndEntity.Entity;
using SqlSugarAndEntity.Base;
using SqlSugarAndEntity.Extension;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using static SqlSugarAndEntity.Extension.DataModel;
using Repository.Base;
using AutoRegistDependency.Attributes;

namespace Repository.Role
{
    [Component]
    public class OrgRepository:BaseRepository<sys_orginfo>, IOrgRepository
    {
        private IBaseMethod baseMethod;
        public OrgRepository(IBaseMethod baseMethod) :base(baseMethod)
        {
            this.baseMethod = baseMethod;
        }

        public PageListModel<sys_orginfo> GetLazyOrgTree(decimal? ParentId,int page = 1, int limit = 1000)
        {
            Expression<Func<sys_orginfo, bool>> exp;
            if (ParentId == null)
            {
                exp = t => t.ParentOrgID == null;
            }
            else
            {
                exp = t => t.ParentOrgID == ParentId;
            }
            var OrgList = baseMethod.Db().Queryable<sys_orginfo>()
                .Where(exp)
                .Select(t => new sys_orginfo
                {
                    ID = t.ID,
                    OrgName = t.OrgName,
                    OrgCode = t.OrgCode,
                    ParentOrgID = t.ParentOrgID,
                    ShortName = t.ShortName,
                    Remark = t.Remark,
                    CreateBy = t.CreateBy,
                    hasChildren = SqlFunc.Subqueryable<sys_orginfo>().Where(a => a.ParentOrgID == t.ID).Any(),
                })
                .Mapper(it =>
                {
                    if (it.hasChildren)
                    {
                        it.children = new List<sys_orginfo>();
                    }
                })
                .ToPageModel(page, limit);
            return OrgList;
                
        }

        public override List<sys_orginfo> GetInfo(Expression<Func<sys_orginfo, bool>> predicate)
        {
            return baseMethod.Db().Queryable<sys_orginfo>().WhereIF(predicate != null, predicate).ToList();
        }
        public override bool Insert(sys_orginfo entity)
        {
            var list1 = baseMethod.Db().Storageable(new sys_orginfo() { ID = 173865813065797 })
                .WhereColumns(t => new{ t.ID }).ToStorage().InsertList.Select(t => t.Item);
            return baseMethod.Db().Insertable(entity).ExecuteCommand() > 0;
        }

        public override bool Insert(List<sys_orginfo> list)
        {
            return baseMethod.Db().Insertable(list).ExecuteCommand()>0;
        }
        public override bool Delete(Expression<Func<sys_orginfo, bool>> WhereExp)
        {
            return baseMethod.Db().Deleteable<sys_orginfo>().Where(WhereExp).ExecuteCommand() > 0;
        }
        public override void Delete(List<sys_orginfo> list)
        {
            baseMethod.Db().Deleteable(list).ExecuteCommand();
        }
        public override bool Update(sys_orginfo entity)
        {
            return baseMethod.Db().Updateable(entity).ExecuteCommand() > 0;
        }
        public override bool Update(sys_orginfo entity, Expression<Func<sys_orginfo, object>> WhereSelect)
        {
            return baseMethod.Db().Updateable(entity).WhereColumns(WhereSelect).ExecuteCommand() > 0;
        }
        public override bool Update(Expression<Func<sys_orginfo, sys_orginfo>> SetColumns, Expression<Func<sys_orginfo, bool>> WhereExp)
        {
            return baseMethod.Db().Updateable(SetColumns).Where(WhereExp).ExecuteCommand() > 0;
        }
        public override bool UpdateIgnoreColumns(sys_orginfo entity, Expression<Func<sys_orginfo, object>> WhereSelect, Expression<Func<sys_orginfo, object>> IgnoreExpress)
        {
            return baseMethod.Db().Updateable(entity).IgnoreColumns(IgnoreExpress).WhereColumns(WhereSelect).ExecuteCommand() > 0;
        }
        public override bool UpdateAppiontColumns(sys_orginfo entity, Expression<Func<sys_orginfo, object>> WhereSelect, Expression<Func<sys_orginfo, object>> UpdateExpress)
        {
            return baseMethod.Db().Updateable(entity).UpdateColumns(UpdateExpress).WhereColumns(WhereSelect).ExecuteCommand() > 0;
        }

        public override bool Update(List<sys_orginfo> list)
        {
            return baseMethod.Db().Updateable(list).ExecuteCommand() > 0;
        }
        public override bool Update(List<sys_orginfo> list, Expression<Func<sys_orginfo, object>> WhereExp)
        {
            return baseMethod.Db().Updateable(list).WhereColumns(WhereExp).ExecuteCommand() > 0;
        }
    }
}
